/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.fatecpg.songs.intranet.managedbeans;

import br.com.fatecpg.songs.crud.facade.RegisterFacade;
import br.com.fatecpg.songs.dao.util.DAOException;
import br.com.fatecpg.songs.entity.Recorder;
import br.com.fatecpg.songs.util.FacesContextUtil;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.hibernate.exception.ConstraintViolationException;

/**
 *
 * @author Eloyza
 */
@ManagedBean
@ViewScoped
public class RecorderManagedBean implements RegisterManagedBean<Recorder>{
    private Recorder recorder;
    private final RegisterFacade<Recorder> facade = new RegisterFacade<Recorder>(Recorder.class);    
    private List<Recorder> list;
    private int viewState;   

    public RecorderManagedBean() throws DAOException{
        recorder = new Recorder();        
        list = facade.list();
        this.viewState = LIST_STATE;
    }        
    
    @Override
    public void openNew() {
        recorder = new Recorder();
        this.viewState = REGISTER_STATE;
    }

    @Override
    public void openToUpdate() throws DAOException{        
        recorder = facade.load();
        this.viewState = REGISTER_STATE;
    }

    @Override
    public void delete() throws DAOException{
        try{
            facade.delete();
            list = facade.list();
            viewState = LIST_STATE;            
        }catch(ConstraintViolationException ex){
            FacesContextUtil.addMessage("Existem albuns desta gravadora. Para deletá-la, é necessário deletar os albums primeiro.");            
        }
    }

    @Override
    public void register() throws DAOException{
        facade.register(recorder);
        list = facade.list();
        viewState = LIST_STATE;
    }

    @Override
    public void cancel() {
        this.viewState = LIST_STATE;
    }     
    
    @Override
    public List<Recorder> getList() {
        return list;
    }
    
    public Recorder getRecorder() {
        return recorder;
    }

    public void setRecorder(Recorder recorder) {
        this.recorder = recorder;
    }

    public int getViewState() {
        return viewState;
    }

    public void setViewState(int viewState) {
        this.viewState = viewState;
    }     
}
